﻿
//-----------------------------------------------------------
// Description: Xử lý các action trong trang quản lý Menu 
//-----------------------------------------------------------
pages_admin_Function = new function () {

    this.htmlTag = new function () {
        this.divNode = '#divNode';
        this.cbxFunctionParent = '.cbxFunctionParent';
        this.cbxFunction = '.cbxFunction';
        this.ulFunction = '#ulFunctionAll';
        this.divFunctionDetail = "#divFunctionDetail";
        this.divFunctionList = "#divFunctionList";
        this.btnDeleteFunction = '#aDeleteFunction';
        this.btnAddFunction = '#aAddFunction';
        this.btnEditFunction = '#aEditFunction';
    };

    this.m_MenuID = 0;

    // type=0: Thêm
    // type=1: Sửa
    this.m_type = 0;

    this.pageLoad = function () {
        //COMMON.loadcssfile('/css/Treeview.css');
        this.loadCategory();
    };

    this.loadCategory = function () {
        Loading.showProcess();
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { t: 'GetListFunction' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $(pages_admin_Function.htmlTag.ulFunction).setTemplateURL('/Template/Pages/Config/ListMenuTreeView.htm');
                $(pages_admin_Function.htmlTag.ulFunction).processTemplate(data);

                pages_admin_Function.bindTreeview(pages_admin_Function.htmlTag.ulFunction);
                Loading.closeProcess();
            }
        });
    };

    this.bindTreeview = function (m_sel) {
        // click vào functionparent
        $(".spNameFunctionParent").live('click', function () {
            if ($("ul", this.parentNode).attr("class") == "visible") {
                $("ul", this.parentNode).slideUp(0, function () {
                    $(this).attr("class", "invisible");
                    $(this.parentNode).attr('class', 'expandable');
                });

            }
            else {
                $("ul", this.parentNode).slideDown(0, function () {
                    $(this).attr("class", "visible");
                    $(this.parentNode).attr('class', 'collapsable');
                });

            }
        });

        // click vào checkbox functionparent
        $(this.htmlTag.cbxFunctionParent, m_sel).live("click", function () {
            if (this.checked) {
                $("li", this.parentNode).find("input[type='checkbox']").attr("checked", "" + this.checked);
                $("ul", this.parentNode).slideDown(0, function () {
                    $(this).attr("class", "visible");
                    $(this.parentNode).attr('class', 'collapsable');
                });
            }
            else {
                $("li", this.parentNode).find("input[type='checkbox']").attr("checked", "");
            }
        });

        // click vào checkbox function
        $(this.htmlTag.cbxFunction, m_sel).live("click", function () {
            if (this.checked) {
                $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "true");
            }
            else {
                if ($(this.parentNode.parentNode).find("input[checked='true']").length == 0) {
                    $(this.parentNode.parentNode.parentNode).children("input[type='checkbox']").attr("checked", "");
                }
            }
        });
    };

    this.bindFunctionParent = function (_parentID) {
        $(".optFunctionParrent").remove();
        if (pages_admin_Function.m_type == 0) {
            $("#ddlFunctionParent").attr('disabled', 'disabled');
        }
        else {
            $("#ddlFunctionParent").attr('disabled', '');
        }
        if (_parentID == -1) {
            $("#ddlFunctionParent").append('<option value="' + '-1' + '" selected="selected" class="optFunctionParrent" >' + 'Menu gốc' + '</option>');
            $("#ddlFunctionParent").attr('disabled', 'disabled');
        }
        else {
            $.ajax({
                type: "GET",
                url: "/Handler/HandlerConfig.ashx",
                data: { t: 'GetListFunctionParrent' },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    for (var i = 0; i < data.Items.length; i++) {
                        if (data.Items[i].Id == _parentID) {
                            $("#ddlFunctionParent").append('<option value="' + data.Items[i].Id + '" selected="selected" class="optFunctionParrent" >' + data.Items[i].Name + '</option>');
                        }
                        else
                            $("#ddlFunctionParent").append('<option value="' + data.Items[i].Id + '" class="optFunctionParrent" >' + data.Items[i].Name + '</option>');
                    }
                }
            });
        }
    };

    this.bindRoleInFunction = function () {
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { menuID: pages_admin_Function.m_MenuID, t: 'GetListRoleInMenu' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#divListRoleInFunction").setTemplateURL("/Template/Pages/Config/ListRoleInMenu.htm");
                $("#divListRoleInFunction").processTemplate(data);
                pages_admin_Function.setRowNumber("#divListRoleInFunction");
                pages_admin_Function.setStyleRow("#divListRoleInFunction");
            }
        });
    };

    this.setRowNumber = function (m_selParent) {
        var m_stt = 0;
        $('#divSTT', m_selParent).each(function () {
            m_stt++;
            $(this).text(m_stt);
        });
    };

    this.setStyleRow = function (m_selParent) {
        var m_stt = 0;
        $('#divItem', m_selParent).each(function () {
            m_stt++;
            if ((m_stt % 2) == 0) {
                $(this.parentNode).addClass('even');
            }
            else {
                $(this.parentNode).addClass('odd');
            }
        });
    };

    $("#aAddFunction").live('click', function () {
        pages_admin_Function.m_MenuID = $(this.parentNode).attr("id").substring(2);
        if (pages_admin_Function.m_MenuID == 0) {
            pages_admin_Function.m_MenuID = -1;
            $("#divFunctionUrl").hide();
        }
        else {
            $("#divFunctionUrl").show();
        }
        pages_admin_Function.m_type = 0; //thêm mới
        $("#txtFunctionName").val('');
        $("#txtFunctionName").focus();
        $("#txtFunctionUrl").val('');
        $("#divFunctionOrder").hide();
        pages_admin_Function.bindFunctionParent(pages_admin_Function.m_MenuID);

        $(".optStatus").remove();
        for (var i = 1; i < 3; i++) {
            $("#ddlStatusFunction").append('<option class="optStatus" value="' + i + '" >' + pages_admin_Function.convertStatus(i).toString() + '</option>');
        }

        $("#divRoleInFuncton").hide();
        pages_admin_Function.displayControl(false);
    });

    $("#aEditFunction").live('click', function () {
        pages_admin_Function.m_MenuID = $(this.parentNode).attr("id").substring(2);
        pages_admin_Function.m_type = 1;
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerConfig.ashx",
            data: { functionID: pages_admin_Function.m_MenuID, t: 'GetFunctionInfo' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#txtFunctionName").focus();
                $("#txtFunctionName").val(data.Name);
                $("#txtFunctionUrl").val(data.URL);
                $("#divFunctionOrder").show();
                $("#txtFunctionOrder").val(data.Order);
                pages_admin_Function.bindFunctionParent(data.ParentId);
                if (data.ParentId > 0) {
                    pages_admin_Function.bindRoleInFunction();
                    $("#divRoleInFuncton").show();
                    $("#divFunctionUrl").show();
                }
                else {
                    $("#divRoleInFuncton").hide();
                    $("#divFunctionUrl").hide();
                }

                $(".optStatus").remove();
                var _status = data.Status;
                for (var i = 1; i < 3; i++) {
                    if (i == _status) {
                        $("#ddlStatusFunction").append('<option class="optStatus" value="' + i + '" selected="selected">' + pages_admin_Function.convertStatus(i).toString() + '</option>');
                    }
                    else {
                        $("#ddlStatusFunction").append('<option class="optStatus" value="' + i + '" >' + pages_admin_Function.convertStatus(i).toString() + '</option>');
                    }
                }

                pages_admin_Function.displayControl(false);
            }
        });
    });

    $("#aUpdatePermission").live('click', function () {
        Loading.showProcess();
        var _roleID = $(this.parentNode).attr("id").substring(3);
        var _per = 0;
        if ($("#cbxPermissionView" + pages_admin_Function.m_MenuID).attr('checked')) {
            _per += 8;
        }
        if ($("#cbxPermissionAdd" + pages_admin_Function.m_MenuID).attr('checked')) {
            _per += 4;
        }
        if ($("#cbxPermissionEdit" + pages_admin_Function.m_MenuID).attr('checked')) {
            _per += 2;
        }
        if ($("#cbxPermissionDelete" + pages_admin_Function.m_MenuID).attr('checked')) {
            _per += 1;
        }

        var _data = {
            roleID: _roleID,
            menuID: pages_admin_Function.m_MenuID,
            permission: _per
        }
        $.ajax({
            type: "POST",
            url: "/WebService/WSConfig.asmx/UpdatePermissionRoleFunction",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                pages_admin_Function.bindRoleInFunction();
                jAlert(MESSAGE.updatePermissionRoleFunctionSuccess, MESSAGE.title);
                Loading.closeProcess();
            }
        });
    });

    $("#aDeleteFunction").live('click', function () {
        pages_admin_Function.m_MenuID = $(this.parentNode).attr("id").substring(2);
        jConfirm(MESSAGE.confirmDeleteMenu, MESSAGE.confirm, function (result) {
            if (result) {
                Loading.showProcess();
                var _data =
                {
                    menuID: pages_admin_Function.m_MenuID
                };
                $.ajax({
                    type: "POST",
                    url: "/WebService/WSConfig.asmx/DeleteMenu",
                    data: JSON.encode(_data),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    cache: false,
                    success: function (data) {
                        if (data.d > 0) {
                            jAlert(MESSAGE.deleteSuccess, MESSAGE.title);
                            pages_admin_Function.loadCategory();
                        }
                        else {
                            if (data.d == -1) {
                                jAlert(MESSAGE.existMenuChild, MESSAGE.title);
                            }
                            else {
                                jAlert(MESSAGE.error, MESSAGE.title);
                            }
                        }
                        Loading.closeProcess();
                    }
                });
            }
        });
    });

    $("#btnMenuUpdate").live('click', function () {
        Loading.showProcess();
        if (pages_admin_Function.m_type == 1) {
            if (($("#txtFunctionName").val() == '') || ($("#txtFunctionOrder").val() == '')) {
                jAlert(MESSAGE.errorInfo, MESSAGE.title);
                return;
            }
            var _data =
                {
                    menuID: pages_admin_Function.m_MenuID,
                    name: $("#txtFunctionName").val(),
                    parrentID: $("#ddlFunctionParent").val(),
                    url: $("#txtFunctionUrl").val(),
                    order: $("#txtFunctionOrder").val(),
                    status: $("#ddlStatusFunction").val()
                };
            $.ajax({
                type: "POST",
                url: "/WebService/WSConfig.asmx/UpdateMenu",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.d > 0) {
                        jAlert(MESSAGE.updateSuccess, MESSAGE.title);
                        pages_admin_Function.loadCategory();
                        pages_admin_Function.displayControl(true);
                    }
                    else {
                        if (data.d == -1) {
                            jAlert(MESSAGE.errorNameFunction, MESSAGE.title);
                        }
                        else {
                            jAlert(MESSAGE.error, MESSAGE.title);
                        }
                    }
                }
            });
        }
        else {
            if ($("#txtFunctionName").val() == '') {
                jAlert(MESSAGE.errorInfo, MESSAGE.title);
                return;
            }
            var _data =
                {
                    name: $("#txtFunctionName").val(),
                    parrentID: $("#ddlFunctionParent").val(),
                    url: $("#txtFunctionUrl").val(),
                    status: $("#ddlStatusFunction").val()
                };
            $.ajax({
                type: "POST",
                url: "/WebService/WSConfig.asmx/AddMenu",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.d > 0) {
                        jAlert(MESSAGE.addSuccess, MESSAGE.title);
                        pages_admin_Function.loadCategory();
                        pages_admin_Function.displayControl(true);
                    }
                    else {
                        if (data.d == -1) {
                            jAlert(MESSAGE.errorNameFunction, MESSAGE.title);
                        }
                        else {
                            jAlert(MESSAGE.error, MESSAGE.title);
                        }
                    }
                }
            });
        }
        Loading.closeProcess();
    });

    $("#btnMenuCancel").live('click', function () {
        pages_admin_Function.displayControl(true);
    });

    $("#aDeleteRoleFunction").live('click', function () {
        var _roleID = $(this.parentNode).attr("id").substring(6);
        jConfirm(MESSAGE.confirmDeleteRoleFromFunction, MESSAGE.confirm, function (result) {
            if (result) {
                Loading.showProcess();
                var _data = {
                    roleID: _roleID,
                    menuID: pages_admin_Function.m_MenuID
                }
                $.ajax({
                    type: "POST",
                    url: "/WebService/WSConfig.asmx/DeleteRoleFunction",
                    data: JSON.encode(_data),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    cache: false,
                    success: function (data) {
                        pages_admin_Function.bindRoleInFunction();
                        jAlert(MESSAGE.deleteSuccess, MESSAGE.title);
                        Loading.closeProcess();
                    }
                });
            }
        });
    });

    $("#btnAddRoleToFunction").live('click', function () {
        ShowListRoleNotInFunction.showPopup(pages_admin_Function.m_MenuID);
    });

    this.displayControl = function (_bool) {
        if (_bool) {
            $(pages_admin_Function.htmlTag.divFunctionDetail).slideUp(0, function () {
                $(pages_admin_Function.htmlTag.divFunctionList).slideDown(0);
            });
        }
        else {
            $(this.htmlTag.divFunctionList).slideUp(0, function () {
                $(pages_admin_Function.htmlTag.divFunctionDetail).slideDown(0);
            });
        }
    };

    this.convertStatus = function (_status) {
        if (_status == 1)
            return MESSAGE.status_active;
        if (_status == 2)
            return MESSAGE.status_lock;
    };

}